<meta charset="UTF-8">
<?php
require ('./lib/init.php');
$art_id = $_GET['art_id'];
$sql = "select cat_name,cat_id from cat";
$cats = mGetAll($sql);
//art_id 是否合法
if (!is_numeric($art_id)){
    header('location:index.php');
}
//判断文章是否存在 没有就跳转到首页
$sql = "select * from art where art_id = $art_id";
if (!mGetRow($sql)){
    header('location:index.php');
}

//查询文章
$sql = "select title,content,author,pubtime,cat_name,comm,pic from art inner join cat on art.cat_id = cat.cat_id where art_id = $art_id";
$art = mGetRow($sql);

//查询所有的留言
$sql = "select nick,pubtime,content from comment where art_id = $art_id";
$comms = mGetAll($sql);


// post 非空 代表有留言
if (!empty($_POST)){
    $comm['nick'] = trim($_POST['nick']);
    $comm['email'] = trim($_POST['email']);
    $comm['content'] = htmlspecialchars(trim($_POST['content']));
    $comm['pubtime'] = time();
    $comm['art_id'] = $art_id;
    $comm['ip'] = sprintf('%u',ip2long(getRealIp()));
    if ($comm['nick'] =='' or $comm['content'] == '') error('昵称和内容不能为空！');
    $rs = mExec('comment',$comm);
    if ($rs){
        //评论发布成功 对应文章的comm数量+1
        $sql = "update art set comm = comm + 1 where art_id = $art_id";
        mQuery($sql);
        //跳转到上个页面
        $ref = $_SERVER['HTTP_REFERER'];
        header("location:$ref ");
    }
}
require (ROOT . '/view/front/art.html');
?>